"use strict";
const { Model } = require("sequelize");

module.exports = (sequelize, DataTypes) => {
  class Role extends Model {
    static associate(models) {
      // 关联到 User 表（如有）
      // Role.hasMany(models.User, { foreignKey: 'roleId' });
      models.Role.hasMany(models.User, { foreignKey: "role", as: "users" });
    }
  }
  Role.init(
    {
      name: {
        type: DataTypes.STRING(30),
        allowNull: false,
        unique: true,
        validate: {
          notEmpty: { msg: "角色名不能为空" },
        },
      },
      description: {
        type: DataTypes.STRING(100),
      },
    },
    {
      sequelize,
      modelName: "Role",
    }
  );
  return Role;
};
